using System;
using System.ComponentModel;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Altairis.Web.UI.WebControls {

    /// <summary>
    /// Represents an <see cref="System.Web.HttpContext.Items" /> parameter.
    /// </summary>
    public class ContextItemParameter : Parameter {

        /// <summary>
        /// Key of item in <see cref="System.Web.HttpContext.Items" /> collection.
        /// </summary>
        [Category("Behavior")]
        public string ContextItemField {
            get { return (string)this.ViewState["ContextItemField"]; }
            set { this.ViewState["ContextItemField"] = value; }
        }

        protected override object Evaluate(HttpContext context, Control control) {
            object itemValue = context.Items[this.ContextItemField];
            if (itemValue == null) return this.DefaultValue;

            try {
                return System.Convert.ChangeType(itemValue, this.Type);
            }
            catch (Exception) {
                return this.DefaultValue;
            }
        }
    }

    /// <summary>
    /// Represents an Membership provider user key parameter.
    /// Returns ProviderUserKey of currently logged in user or <see cref="DefaultValue"/> for anonymous users.
    /// </summary>
    public class ProviderUserKeyParameter : Parameter {

        protected override object Evaluate(HttpContext context, Control control) {
            if (!context.Request.IsAuthenticated) return this.DefaultValue;
            return System.Web.Security.Membership.GetUser().ProviderUserKey;
        }
    }

    public class RoleMembershipParameter : Parameter {

        /// <summary>
        /// Gets or sets the name of the role.
        /// </summary>
        /// <value>The name of the role.</value>
        [Category("Behavior")]
        public string RoleName {
            get { return (string)this.ViewState["RoleName"]; }
            set { this.ViewState["RoleName"] = value; }
        }

        protected override object Evaluate(HttpContext context, Control control) {
            if (!context.Request.IsAuthenticated) return false;
            return context.User.IsInRole(this.RoleName);
        }
    }

    /// <summary>
    /// Represents an HTTP querystring parameter.
    /// If parameter is in incorrect format and cannot be casted to correct type, <see cref="DefaultValue"/> is returned.
    /// </summary>
    public class SafeQueryStringParameter : QueryStringParameter {

        protected override object Evaluate(HttpContext context, Control control) {
            try {
                return System.Convert.ChangeType(base.Evaluate(context, control), this.Type);
            }
            catch (Exception) {
                return this.DefaultValue;
            }
        }
    }

    public class UiCultureParameter : Parameter {

        public UiCultureParameter() {
            this.SpecificationType = CultureSpecificationType.Name;
        }

        [Category("Behavior")]
        public CultureSpecificationType SpecificationType {
            get { return (CultureSpecificationType)this.ViewState["SpecificationType"]; }
            set { this.ViewState["SpecificationType"] = value; }
        }

        protected override object Evaluate(HttpContext context, Control control) {
            CultureInfo culture = System.Threading.Thread.CurrentThread.CurrentUICulture;
            switch (this.SpecificationType) {
                case CultureSpecificationType.TwoLetterISOLanguageName:
                    return culture.TwoLetterISOLanguageName;
                case CultureSpecificationType.ThreeLetterISOLanguageName:
                    return culture.ThreeLetterISOLanguageName;
                case CultureSpecificationType.ThreeLetterWindowsLanguageName:
                    return culture.ThreeLetterWindowsLanguageName;
                case CultureSpecificationType.EnglishName:
                    return culture.EnglishName;
                default:
                    return culture.Name;
            }
        }
    }

    public class UserAgentParameter : Parameter {

        protected override object Evaluate(HttpContext context, Control control) {
            return context.Request.UserAgent;
        }
    }

    public class UserHostAddressParameter : Parameter {

        protected override object Evaluate(HttpContext context, Control control) {
            return context.Request.UserHostAddress;
        }
    }

    /// <summary>
    /// Represents an Membership user name parameter.
    /// Returns user name of currently logged in user or <see cref="DefaultValue"/> for anonymous users.
    /// </summary>
    public class UserNameParameter : Parameter {

        protected override object Evaluate(HttpContext context, Control control) {
            if (!context.Request.IsAuthenticated) return this.DefaultValue;
            return context.User.Identity.Name;
        }
    }
}